package com.injurydetection.www.adapter;

import android.app.Service;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.injurydetection.www.MyApplication;
import com.injurydetection.www.R;
import com.injurydetection.www.bean.DataBean;
import com.injurydetection.www.bean.JianshangMain;
import com.injurydetection.www.greendao.DataBeanDao;
import com.injurydetection.www.util.DateUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by sunguiyong on 2020/8/12
 */
public class JianceMainAdapter extends RecyclerView.Adapter<JianceMainAdapter.MyViewHolder> {
    private List<JianshangMain> mJianshangMainList;

    public List<DataBean> getDataBeans() {
        return mDataBeans;
    }

    public void setDataBeans(List<DataBean> dataBeans) {
        mDataBeans = dataBeans;
    }

    private List<DataBean> mDataBeans;
    private List<Boolean> mBooleans = new ArrayList<>();
    private Context mContext;
    private boolean showCheckBox;
    private static final String TAG = "JianceMainAdapter";
    private OnLongClickCallBack onLongClickCallBack;

    public OnItemClick getOnItemClick() {
        return mOnItemClick;
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        mOnItemClick = onItemClick;
    }

    private OnItemClick mOnItemClick;
    //防止checkbox错乱，做setTag  getTag操作
    private SparseBooleanArray mCheckStates = new SparseBooleanArray();

    private HashMap<Integer, Boolean> map;


    public JianceMainAdapter(List<DataBean> dataBeans, Context context) {
        this.mDataBeans = dataBeans;
        mContext = context;
        for (int i = 0; i < mDataBeans.size(); i++) {
            mBooleans.add(false);
        }

    }

    //全选
    public void selectAll() {
        Set<Map.Entry<Integer, Boolean>> entries = map.entrySet();
        boolean shouldAll = false;
        for (Map.Entry<Integer, Boolean> entry : entries) {
            boolean value = entry.getValue();
            if (!value) {
                shouldAll = true;
                break;
            }
        }
        for (Map.Entry<Integer, Boolean> entry : entries) {
            entry.setValue(shouldAll);
        }
        notifyDataSetChanged();
    }

    //反选
    public void neverall() {
        Set<Map.Entry<Integer, Boolean>> entries = map.entrySet();
        for (Map.Entry<Integer, Boolean> entry : entries) {
            entry.setValue(!entry.getValue());
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_jianshang_main, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.mCheckBox.setTag(position);
        holder.name.setText(mDataBeans.get(position).getName());
        holder.number.setText(mDataBeans.get(position).getNumber());
        holder.xinlv.setText(mDataBeans.get(position).getHeartRate() + "");
        holder.xueyang.setText(mDataBeans.get(position).getBloodOxygen() + "");
        holder.huxilv.setText(mDataBeans.get(position).getRespiratoryRate() + "");
        holder.xueya.setText(mDataBeans.get(position).getBloodPressureH() + "/" + mDataBeans.get(position).getBloodPressureL());
        holder.xuexing.setText("血型：" + mDataBeans.get(position).getBloodType());
        String eye = "自动睁眼";
        switch (mDataBeans.get(position).getEye()) {
            case 1:
                eye = "不睁眼";
                break;
            case 2:
                eye = "刺痛睁眼";
                break;
            case 3:
                eye = "呼吸睁眼";
                break;
        }
        holder.eye.setText(eye);

        String yuyan = "回答切题";
        switch (mDataBeans.get(position).getSay()) {
            case 1:
                yuyan = "不能言语";
                break;
            case 2:
                yuyan = "只能发言";
                break;
            case 3:
                yuyan = "答非所问";
                break;
            case 4:
                yuyan = "回答不切题";
                break;
        }
        holder.yuyan.setText(yuyan);

        String yundong = "按吩咐动作";
        switch (mDataBeans.get(position).getSport()) {
            case 1:
                yundong = "不能活动";
                break;
            case 2:
                yundong = "刺痛后肢体能过度伸展";
                break;
            case 3:
                yundong = "刺痛后肢体能屈曲";
                break;
            case 4:
                yundong = "刺痛后能躲避";
                break;
            case 5:
                yundong = "刺痛能定位";
                break;
        }
        holder.dongzuo.setText(yundong);


        int shangqingLevel = mDataBeans.get(position).getScore();
        String shanghai = "";
        String chuli = "";
        if (shangqingLevel >= 12) {
            shanghai = "轻伤";
            chuli = "常规处置";
            holder.score.setTextColor(Color.GREEN);
            holder.score.setBackgroundColor(Color.parseColor("#292930"));
        } else if (shangqingLevel >= 10) {
            shanghai = "中度伤";
            chuli = "优先处置";
            holder.score.setTextColor(Color.YELLOW);
            holder.score.setBackgroundColor(Color.parseColor("#292930"));
        } else if (shangqingLevel >= 6) {
            shanghai = "重症伤";
            chuli = "紧急处置";
            holder.score.setTextColor(Color.RED);
            holder.score.setBackgroundColor(Color.parseColor("#292930"));
        } else {
            shanghai = "危重症伤";
            chuli = "期待处置";
            holder.score.setTextColor(Color.BLACK);
            holder.score.setBackgroundColor(Color.RED);
        }

        holder.score.setText(mDataBeans.get(position).getScore() + "分\u3000" + shanghai + "\u3000" + chuli);
        String time = DateUtils.stampToDate(mDataBeans.get(position).getCreateTime().getTime());
        holder.time.setText(time);

        //判断当前checkbox的状态
        if (showCheckBox) {
            holder.mCheckBox.setVisibility(View.VISIBLE);
            //防止显示错乱
            holder.mCheckBox.setChecked(mCheckStates.get(position, false));
        } else {
            holder.mCheckBox.setVisibility(View.INVISIBLE);
            //取消掉Checkbox后不再保存当前选择的状态
            holder.mCheckBox.setChecked(false);
            mCheckStates.clear();
        }
        //对checkbox的监听 保存选择状态 防止checkbox显示错乱
        holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mBooleans.set(position, isChecked);
                int pos = (int) buttonView.getTag();
                map.put(position, isChecked);
                if (isChecked) {
                    mCheckStates.put(pos, true);
                } else {
                    mCheckStates.delete(pos);
                }
            }
        });
        if (map != null) {
            if (map.get(position) == null) {
                map.put(position, false);
            }
            holder.mCheckBox.setChecked(map.get(position));
        }

        holder.jianshangmain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClick.onItemClick(position);

                Log.e(TAG, "onClick:position " + position + "===" + getDataBeans().size());
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDataBeans == null ? 0 : mDataBeans.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        CheckBox mCheckBox;
        TextView name, number, xinlv, xueyang, huxilv, xueya, eye, yuyan, dongzuo, score, time, xuexing;
        ConstraintLayout jianshangmain;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            mCheckBox = itemView.findViewById(R.id.checkbox);
            name = itemView.findViewById(R.id.name_tv);
            number = itemView.findViewById(R.id.num_tv);
            xinlv = itemView.findViewById(R.id.xinlv);
            xueyang = itemView.findViewById(R.id.xueyang);
            huxilv = itemView.findViewById(R.id.huxilv);
            xueya = itemView.findViewById(R.id.xueya);
            eye = itemView.findViewById(R.id.zhengyan);
            yuyan = itemView.findViewById(R.id.yuyan);
            dongzuo = itemView.findViewById(R.id.dongzuo);
            score = itemView.findViewById(R.id.pingfen);
            time = itemView.findViewById(R.id.time);
            jianshangmain = itemView.findViewById(R.id.jianshangmain);
            xuexing = itemView.findViewById(R.id.bloodtype_tv);

            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onLongClickCallBack.onLongClick();
                    map = new HashMap<>();
                    for (int i = 0; i < mDataBeans.size(); i++) {
                        map.put(i, false);
                    }
                    return true;
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }
    }

    public interface OnLongClickCallBack {
        void onLongClick();
    }

    public interface OnItemClick {
        void onItemClick(int position);
    }

    public OnLongClickCallBack getOnLongClickCallBack() {
        return onLongClickCallBack;
    }

    public void setOnLongClickCallBack(OnLongClickCallBack onLongClickCallBack) {
        this.onLongClickCallBack = onLongClickCallBack;
    }

    public boolean isShowCheckBox() {
        return showCheckBox;
    }

    public void setShowCheckBox(boolean showCheckBox) {
        this.showCheckBox = showCheckBox;
    }

    /**
     * 删除选中的数据
     */
    public void deleteData() {
        try {
            mDataBeans = MyApplication.getDaoSession().getDataBeanDao().queryBuilder().orderDesc(DataBeanDao.Properties.CreateTime).build().list();
            notifyDataSetChanged();
            int y = 0;
            for (int i = 0; i < mDataBeans.size(); i++) {//1
                if (mBooleans.size() == 0) {
                    mDataBeans = MyApplication.getDaoSession().getDataBeanDao().queryBuilder().orderDesc(DataBeanDao.Properties.CreateTime).build().list();
                    notifyDataSetChanged();
                    return;
                }
                if (mBooleans.get(i) != null && mBooleans.get(i)) {
                    MyApplication.getDaoSession().getDataBeanDao().delete(mDataBeans.get(i));
                    mDataBeans = MyApplication.getDaoSession().getDataBeanDao().queryBuilder().orderDesc(DataBeanDao.Properties.CreateTime).build().list();
                    notifyDataSetChanged();
                    mDataBeans.remove(i);
                    mBooleans.remove(i);
                    y++;
                    i--;
                }
            }
            mDataBeans = MyApplication.getDaoSession().getDataBeanDao().queryBuilder().orderDesc(DataBeanDao.Properties.CreateTime).build().list();
            notifyDataSetChanged();

            if (y == 0) {
                Toast.makeText(mContext, "没有选中的要删除的数据", Toast.LENGTH_SHORT).show();
            }
        } catch (IndexOutOfBoundsException e) {

        }
        notifyDataSetChanged();


    }
}
